package com.sky.aspect;

import com.sky.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class AutoFillTimeAspect {

    /**
     * 环绕通知
     * @param joinPoint
     * @return
     */
    @Around("execution(* com.sky.controller.*.*.*(..))&& @annotation(com.sky.annotation.AutoFillTime)")
    public Object AutoFill2(ProceedingJoinPoint joinPoint) throws Throwable {
        //方法执行前毫秒值
        long begin = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        //方法执行后毫秒值
        long end = System.currentTimeMillis();
        //统计耗时
        long time =  end-begin;

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取方法名
        String name = signature.getMethod().getName();
        //当前执行人id
        Long currentId = BaseContext.getCurrentId();
        log.info("当前执行人{}",currentId);
        log.info("{}方法正在执行...",name);
        log.info("方法执行耗时：{}",time);
        return proceed;
    }
}
